package org.jeecg.modules.water.controller;

import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.basic.demo.apartment.entity.LwgyBaseApartment;
import org.jeecg.modules.basic.demo.apartment.service.ILwgyBaseApartmentService;
import org.jeecg.modules.water.entity.LwgyJointWaterCon;
import org.jeecg.modules.water.entity.LwgyJointWaterConfiguration;
import org.jeecg.modules.water.service.ILwgyJointWaterConfigurationService;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.water.entity.LwgyJointWaterDaily;
import org.jeecg.modules.water.entity.LwgyJointWaterCmd;
import org.jeecg.modules.water.service.ILwgyJointWaterDailyService;
import org.jeecg.modules.water.service.ILwgyJointWaterCmdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 监控
 * @Author: jeecg-boot
 * @Date:   2024-04-02
 * @Version: V1.0
 */
@Api(tags="监控")
@RestController
@RequestMapping("/water/lwkjJointWaterCon")
@Slf4j
public class LwkjJointWaterConController {

	 @Autowired
	 private ILwgyJointWaterDailyService lwgyJointWaterDailyService;
	 @Autowired
	 private ILwgyJointWaterCmdService lwgyJointWaterCmdService;
	 @Autowired
	 private ILwgyJointWaterConfigurationService lwgyJointWaterConfigurationService;
	 @Autowired
	 private ILwgyBaseApartmentService lwgyBaseApartmentService;
	/**
	 * 分页列表查询
	 *
	 * @param lwgyJointWaterCon
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "监控-分页列表查询")
	@ApiOperation(value="监控-分页列表查询", notes="监控-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(LwgyJointWaterCon lwgyJointWaterCon,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
/*		QueryWrapper<LwkjJointWaterCon> queryWrapper = QueryGenerator.initQueryWrapper(lwkjJointWaterCon, req.getParameterMap());
		Page<LwkjJointWaterCon> page = new Page<LwkjJointWaterCon>(pageNo, pageSize);
		IPage<LwkjJointWaterCon> pageList = lwkjJointWaterConService.page(page, queryWrapper);
		return Result.OK(pageList);*/
		String type = null;
		String id = lwgyJointWaterCon.getRoomId();
		if (oConvertUtils.isNotEmpty(id)) {
			LwgyBaseApartment lwgyBaseApartment = lwgyBaseApartmentService.getById(id);
			type = lwgyBaseApartment.getType();
		}
		lwgyJointWaterCon.setRoomId(null);
		QueryWrapper<LwgyJointWaterCon> queryWrapper = QueryGenerator.initQueryWrapper(lwgyJointWaterCon, req.getParameterMap());
		if (id != null && type != null) {
			switch (type) {
				case "campus":
					queryWrapper.eq("t.campus_id",id);
					break;
				case "building":
					queryWrapper.eq("t.building_id",id);
					break;
				case "unit":
					queryWrapper.eq("t.unit_id",id);
					break;
				case "layer":
					queryWrapper.eq("t.layer_id",id);
					break;
				case "room":
					queryWrapper.eq("t.room_id",id);
					break;
				default:
					break;
			}
		}
		queryWrapper.orderByDesc("create_time");
		Page<LwgyJointWaterCon> page = new Page<LwgyJointWaterCon>(pageNo, pageSize);
		IPage<LwgyJointWaterCon> pageList = lwgyJointWaterConfigurationService.queryWaterDeviceChartList(page, queryWrapper);
		return Result.OK(pageList);
	}


	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "日用水量通过主表ID查询")
	@ApiOperation(value="日用水量主表ID查询", notes="日用水量-通主表ID查询")
	@GetMapping(value = "/queryLwgyJointWaterDailyByMainId")
	public Result<?> queryLwgyJointWaterDailyListByMainId(@RequestParam(name="id",required=true) String id) {
		LwgyJointWaterConfiguration lwgyJointWaterConfiguration = lwgyJointWaterConfigurationService.getById(id);
		List<LwgyJointWaterDaily> lwgyJointWaterDailyList = lwgyJointWaterDailyService.selectByMainId(lwgyJointWaterConfiguration.getCommunicationCode());
		return Result.OK(lwgyJointWaterDailyList);
	}
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "阀控记录通过主表ID查询")
	@ApiOperation(value="阀控记录主表ID查询", notes="阀控记录-通主表ID查询")
	@GetMapping(value = "/queryLwgyJointWaterCmdByMainId")
	public Result<?> queryLwgyJointWaterCmdListByMainId(@RequestParam(name="id",required=true) String id,

														@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
														@RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
		LwgyJointWaterConfiguration lwgyJointWaterConfiguration = lwgyJointWaterConfigurationService.getById(id);
		//List<LwgyJointWaterCmd> lwgyJointWaterCmdList = lwgyJointWaterCmdService.selectByMainId(lwgyJointWaterConfiguration.getCommunicationCode());

		QueryWrapper<LwgyJointWaterCmd> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("ispid",lwgyJointWaterConfiguration.getCommunicationCode());
		queryWrapper.orderByDesc("create_time");
		Page<LwgyJointWaterCmd> page = new Page<>(pageNo, pageSize);
		//List<LwgyJointWaterCmd> lwgyJointWaterCmdList = lwgyJointWaterCmdService.list(queryWrapper);
		IPage<LwgyJointWaterCmd> pageList = lwgyJointWaterCmdService.page(page, queryWrapper);
		return Result.OK(pageList);
			//return Result.OK(lwgyJointWaterCmdList);
	}



}
